package com.star.rpc.registry;

import com.star.rpc.config.RegistryConfig;
import com.star.rpc.modal.RpcRequest;
import com.star.rpc.modal.ServiceMetaInfo;
import com.star.rpc.serializer.HessianSerializer;

import java.util.List;

/**
 * packageName com.star.rpc.registry
 *
 * @author zerostart
 * @className RegistryTest
 * @date 2024/11/24
 * @description 注册中心测试
 */
public class RegistryTest {

    final Registry registry = new EtcdRegistry();


    public void init() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("http://localhost:2379");
        registry.init(registryConfig);
    }


    public void register() throws Exception {
        ServiceMetaInfo serviceMetaInfo = new ServiceMetaInfo();

        serviceMetaInfo.setServiceName("myService");
        serviceMetaInfo.setServiceVersion("1.0");
        serviceMetaInfo.setServiceHost("localhost");
        serviceMetaInfo.setServicePort(1234);
        registry.register(serviceMetaInfo);

        serviceMetaInfo.setServiceName("myService");
        serviceMetaInfo.setServiceVersion("1.0");
        serviceMetaInfo.setServiceHost("localhost");
        serviceMetaInfo.setServicePort(1235);
        registry.register(serviceMetaInfo);

        serviceMetaInfo.setServiceName("myService");
        serviceMetaInfo.setServiceVersion("1.0");
        serviceMetaInfo.setServiceHost("localhost");
        serviceMetaInfo.setServicePort(1235);
        registry.register(serviceMetaInfo);
    }

    public void unRegister() {
        ServiceMetaInfo serviceMetaInfo = new ServiceMetaInfo();
        serviceMetaInfo.setServiceName("myService");
        serviceMetaInfo.setServiceVersion("1.0");
        serviceMetaInfo.setServiceHost("localhost");
        serviceMetaInfo.setServicePort(1234);
        registry.unRegister(serviceMetaInfo);
    }

    public void ServiceDiscovery() {
        ServiceMetaInfo serviceMetaInfo = new ServiceMetaInfo();

        serviceMetaInfo.setServiceName("myService");
        serviceMetaInfo.setServiceVersion("1.0");
        String serviceKey = serviceMetaInfo.getServiceKey();
        List<ServiceMetaInfo> serviceMetaInfos = registry.serviceDiscovery(serviceKey);
    }

    public static void main(String[] args) throws Exception {
        HessianSerializer hessianSerializer = new HessianSerializer();
        RpcRequest rpcRequest = new RpcRequest();
        rpcRequest.setServiceName("张三");
        rpcRequest.setMethodName("李四");


        byte[] serialize = hessianSerializer.serialize(rpcRequest);


        RpcRequest deserialize = hessianSerializer.deserialize(serialize, RpcRequest.class);
        System.out.println(rpcRequest);


    }

}
